﻿using System;

namespace Basefr.Runtime.Extensions
{
    public static class NumberExtensions
    {
        /// <summary>
        /// 将十进制Int转为34进制（英文字母去掉I、O，防止与1、0混淆）
        /// </summary>
        public static string To34System(this int val, int minLength = 4)
        {
            const string source = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
            var result = string.Empty;

            var tempVal = Math.Abs(val);

            if (tempVal == 0)
            {
                return "0";
            }

            while (tempVal != 0)
            {
                result = source[tempVal % 34] + result;
                tempVal = tempVal / 34;
            }

            if (result.Length < minLength)
            {
                var len = minLength - result.Length;

                for (var i = 0; i < len; i++)
                {
                    result = "0" + result;
                }
            }
            if (val < 0)
            {
                result = "-" + result;
            }

            return result;
        }
    }
}
